<?php

Raise::load('control.form.RaiseFormField');
Raise::load('core.collection.RaiseTypedCollection');
Raise::load('core.postback.RaisePostbackFeedback');
Raise::load('core.ui.RaiseHtml');
Raise::load('core.ui.RaiseHtmlTag');

/**
 * RaiseTextField UIControl class
 * A text field UI control
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.control.form
 * @since 1.2
 */
class RaiseTextField extends RaiseFormField {
    
    /**
     * Specify whether this text field is multiline or not.
     * @var boolean
     */
    private $multiline = false;

    /**
     * Create a new RaiseTextField
     * @param string $name The name of this text field
     * @param string $value (optional) The value of this text field
     * @param boolean $multiline (optional) Specifiy whether this text field is multiline or not
     */
    public function __construct($name, $value = '', $multiline = false) {
        $this->tag = new RaiseHtmlTag('input');
        $this->clientId($name);
        $this->multiline($multiline);
        $this->tag->children(new RaiseCollection());
        $this->tag->children()->add(0, $value);
        $this->renderTag();
    }

    /**
     * Get or set the value of the text field
     * @param string $v (optional) If passed in, the new value will be set.
     * @return string
     */
    public function value($v = false){
        if(func_num_args() == 1){
            if($this->multiline){
                $this->tag()->children()->add(0, $v);
            }else{
                $this->tag()->attributes()->add('value', $v);
            }
        }
        if($this->multiline){
            return $this->tag()->children()->get(0);
        }else{
            $this->tag()->attributes()->get('value');
        }
    }

    /**
     * Get or set whether this text field is multiline or not.
     * @param boolean $m (optional) Set whether this text field is multiline or not.
     * @return boolean
     */
    public function multiline($m = null) {
        if (func_num_args() == 1) {
            $this->multiline = $m;
            if($this->multiline){
                $this->tag->tagName('textarea');
                $this->tag()->children->add(0, $this->tag()->attributes()->get('value'));
                $this->tag()->attributes()->remove('value');
            }else{
                $this->tag->tagName('input');
                $this->tag()->attributes()->add('value', $this->tag->children()->get(0));
                $this->tag->children(null);
            }
        }
        return $this->multiline;
    }
    
    /**
     * Prepare RaiseTextField for multiline textbox
     * @param string $value The value of the text field
     */
    private function prepareMultiline($value){
        $this->tag->value($value);
        $this->tag->attributes()->remove('type');
        if($this->clientId()){
            $this->tag->attributes()->add('id', $this->clientId());
            $this->tag->attributes()->add('name', $this->clientId());
        }
        if ($this->cssClass()) {
            $this->tag->attributes()->add('class', $this->cssClass());
        }
    }
    
    /**
     * Prepare RaiseTextField for normal single line textbox
     * @param string $value The value of the text field
     */
    private function prepareNormal($value){
        $this->tag->attributes()->add('type', 'text');
        if($this->clientId()){
            $this->tag->attributes()->add('id', $this->clientId());
            $this->tag->attributes()->add('name', $this->clientId());
        }
        if ($this->cssClass()) {
            $this->tag->attributes()->add('class', $this->cssClass());
        }
        if ($this->value()) {
            $this->tag->attributes()->add('value', $value);
            $this->tag->value('');
        }
    }
    
    protected function renderTag() {
        $value = RaiseHtml::encode($this->tag->children()->get(0));
        if($this->page() && $this->page()->application() && $this->page()->application()->session()){
            $feedbackList = $this->page()->application()->session()->get(RaisePostbackFeedback::SESSION_KEY);
            if ($feedbackList instanceof RaiseTypedCollection && $feedbackList->type() == 'RaiseFieldFeedback' && $feedbackList->keyExists($this->clientId())) {
                $value = RaiseHtml::encode($feedbackList->get($this->clientId())->value());
            }
        }
        if ($this->multiline) {
            $this->prepareMultiline($value);
        } else {
            $this->prepareNormal($value);
        }
    }

    public function output() {
        $this->renderTag();
        return $this->tag->html();
    }

}

